package com.neutech.web;

import com.neutech.dao.UserDAO;
import com.neutech.dao.impl.UserDAOImpl;
import com.neutech.entity.User;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


/**
 * @author qiqi
 * @create -03-23-9:15
 */
@WebServlet(name = "userServlet",urlPatterns = {"*.user"})
public class UserServlet extends HttpServlet{

    private UserDAO dao = new UserDAOImpl();
    public UserServlet(){
        System.out.println("对象已经实例化");
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("findAll()...");
//        findAll(response);

        //进行前端用户请求的拦截判断
        //根据用户请求url不同，进行不同的业务逻辑操作
        String path = request.getRequestURL().toString();
//        String pathUri = request.getRequestURI();
//        System.out.println(path.substring(path.lastIndexOf("/"),path.lastIndexOf(".")));
        path = path.substring(path.lastIndexOf("/"),path.lastIndexOf("."));
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
//        System.out.println(pathUri);
        if ("/findAll".equals(path)){
            //查询所有
            List<User> users = dao.selectAll();
//            System.out.println(users);
//            向前台输出数据
            //转发：业务逻辑处理未完成，交给更便于编写样式，动效的jsp页面来显示数据
            //1.绑定数据
            request.setAttribute("users",users);
            //2.获取转发器
            /*RequestDispatcher rd = request.getRequestDispatcher("user_list.jsp");
            //3.转发
            rd.forward(request,response);*/

            request.getRequestDispatcher("user_list.jsp").forward(request,response);



            /*out.print("<table align='center' with='80%' cellspacing='0' cellpadding='0'>");
            out.print("<tr><th>ID</th><th>用户名</th><th>密码</th><th>电话</th><th>地址</th><th>注册时间</th><th>修改时间</th><th>状态</th></tr>");
            for (User user:users){
                out.print("<tr>" +
                        "<td>"+user.getId()+"</td>" +
                        "<td>"+user.getName()+"</td>" +
                        "<td>"+user.getPassword()+"</td>" +
                        "<td>"+user.getPhone()+"</td>" +
                        "<td>"+user.getAddress()+"</td>" +
                        "<td>"+user.getCreateTime()+"</td>" +
                        "<td>"+user.getUpdateTIme()+"</td>" +
                        "<td>"+user.getStatus()+"</td>" +
                        "</tr>");
            }
            out.print("</table>");*/
        }else if ("/insert".equals(path)){
            //从前端获取表单提交过来的数据
            String name = request.getParameter("name");
            String password = request.getParameter("password");
            String phone = request.getParameter("phone");
            String address = request.getParameter("address");
            //添加新用户
            dao.insert(new User(name,password,phone,address));

            //重定向到列表页面
            response.sendRedirect("findAll.user");
        }else if ("/delete".equals(path)){
            int id = Integer.parseInt(request.getParameter("id"));
            //删除
            dao.delete(id);
            //重定向到列表页面
            response.sendRedirect("findAll.user");
        }else if ("/load".equals(path)){
            int id = Integer.parseInt(request.getParameter("id"));
            User user = dao.selectById(id);
            request.setAttribute("user",user);
            request.getRequestDispatcher("update_user.jsp").forward(request,response);
        }else if ("/update".equals(path)){
            int id = Integer.parseInt(request.getParameter("id"));
            User user = dao.selectById(id);
            //user对象的属性设置
            String name = request.getParameter("name");
            user.setName(name);
            String password = request.getParameter("password");
            user.setPassword(password);
            String phone = request.getParameter("phone");
            user.setPhone(phone);
            String address = request.getParameter("address");
            user.setAddress(address);
            dao.update(user);
            response.sendRedirect("findAll.user");
        }else{
//            System.out.println("路径错误。。。");
//            response.getWriter().print("<h1>404</h1>");
            //重定向：义务逻辑处理完成之后，页面跳转的过程
            //只要没有匹配到固定的若干url，都会重定向到404.jsp
            response.sendRedirect("404.jsp");
        }
        /*else if (){
            //根据id查找
        }else if (){
            //更新
        }*/
    }

    public void findAll(HttpServletResponse response) throws  IOException{
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        UserDAO dao =new UserDAOImpl();
        List<User> users =dao.selectAll();
        for (User user : users){
            out.print("<p>" +user+ "</p>");
        }
    }
    public void insert(){
        UserDAO dao = new UserDAOImpl();
        dao.insert(new User("","","",""));
    }
}
